/** @file    DataHash.cpp
 *  @author  Alessandro Polo
 *  @version $Id: DataHash.cpp 2628 2010-06-08 23:30:40Z alex $
 *  @brief
 * File containing methods for the wosh::DataHash class.
 * The header for this class can be found in DataHash.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <core/DataHash.h>

 #include <core/Serialization.h>
 #include <cstring>

#ifdef WOSH_CORE_UTILS
 #include <core/utils/polarssl/polarssl_md5.h>
 #include <core/utils/polarssl/polarssl_sha2.h>
 #include <core/utils/polarssl/polarssl_sha4.h>
#endif

using namespace std;
namespace wosh {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT DataHashContext::calculateHash( const ISerializable* object, char* output_digest ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	init();
	bool updated = object->updateHash(this);
	bool finished = finish( output_digest );
	if ( updated && finished ) return WRET_OK;
	return WRET_ERR_INTERNAL;
 }

WRESULT DataHashContext::calculateHash( const ISerializable* object, std::string& output_digest ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	init();
	char* buffer = new char[getDigestDataSize()];
	bool updated = object->updateHash(this);
	bool finished = finish( buffer );
	output_digest.assign(buffer);
	memset( buffer, 0, getDigestDataSize() );
	delete [] buffer;
	if ( updated && finished ) return WRET_OK;
	return WRET_ERR_INTERNAL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DataHashContext_MD5::~DataHashContext_MD5() {
	clear();
 }

void DataHashContext_MD5::clear() {
	if ( this->context != NULL ) {
		memset( this->context, 0, sizeof(md5_context) );
		delete this->context; this->context = NULL;
	 }
 }

void DataHashContext_MD5::init() {
	clear();
	this->context = new md5_context();
	md5_starts( this->context );
 }

bool DataHashContext_MD5::update( const char* input, int size ) {
	if ( this->context == NULL ) return false;
	md5_update( this->context, (const unsigned char*)input, size );
	return true;
 }

bool DataHashContext_MD5::finish( char* output_digest ) {
	if ( this->context == NULL || output_digest == NULL ) return false;
	md5_finish( this->context, (unsigned char*)output_digest );
	clear();
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DataHashContext_SHA256::~DataHashContext_SHA256() {
	clear();
 }

void DataHashContext_SHA256::clear() {
	if ( this->context != NULL ) {
	    memset( this->context, 0, sizeof(sha2_context) );
		delete this->context; this->context = NULL;
	 }
 }

void DataHashContext_SHA256::init() {
	clear();
	this->context = new sha2_context();
	sha2_starts( this->context, this->is224 );
 }

bool DataHashContext_SHA256::update( const char* input, int size ) {
	if ( this->context == NULL ) return false;
	sha2_update( this->context, (const unsigned char*)input, size );
	return true;
 }

bool DataHashContext_SHA256::finish( char* output_digest ) {
	if ( this->context == NULL || output_digest == NULL ) return false;
	sha2_finish( this->context, (unsigned char*)output_digest );
	clear();
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DataHashContext_SHA512::~DataHashContext_SHA512() {
	clear();
 }

void DataHashContext_SHA512::clear() {
	if ( this->context != NULL ) {
		memset( this->context, 0, sizeof(sha4_context) );
		delete this->context; this->context = NULL;
	 }
 }

void DataHashContext_SHA512::init() {
	clear();
	this->context = new sha4_context();
	sha4_starts( this->context, this->is384 );
 }

bool DataHashContext_SHA512::update( const char* input, int size ) {
	if ( this->context == NULL ) return false;
	sha4_update( this->context, (const unsigned char*)input, size );
	return true;
 }

bool DataHashContext_SHA512::finish( char* output_digest ) {
	if ( this->context == NULL || output_digest == NULL ) return false;
	sha4_finish( this->context, (unsigned char*)output_digest );
	clear();
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
